<template>
  <BasicModal @register="register" :title="title" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
    <!-- <p style="color: red; margin-left: 20px"> 提示：将会修改全部计划交付日期 </p> -->
  </BasicModal>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsWhRd02Rd02List',
  });
</script>
<script setup lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm , useForm } from '/@/components/Form';
  import { basPositionTreeData } from '/@/api/bas/pos/basPosition';
  import { savePdQtyData } from '/@/api/wms/wh/pd/whPdStockBill';
  const emit = defineEmits(['success', 'register', 'modifyjfdate']);
  const { showMessage } = useMessage();
  let record = ref<any>({});
  const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm({
    labelWidth: 80,
    schemas: [
      {
        label: '实盘数',
        field: 'frealqty',
        component: 'InputNumber',
        required: true,
        componentProps: {
          maxlength: 255,
        },
      },
      {
        label: '货位',
        field: 'poscode',
        component: 'TreeSelect',
        required: true,
        componentProps: {
          api: basPositionTreeData,
          allowClear: true,
          canSelectParent: false,
          // maxlength: 255,
        },
      },
    ],
    baseColProps: { lg: 24, md: 24 },
  });
  const [register, { closeModal, setModalProps }] = useModalInner(async (data) => {
    record.value = data;
    await resetFields();

    // 判断是否为批量编辑
    if (data.selectedRows) {
      // 批量编辑模式
      console.log('批量编辑选中的行:', data.selectedRows);
      console.log('选中的行键:', data.selectedKeys);

      title.value = `批量编辑 (${data.selectedRows.length} 条记录)`;
    } else {
      // 单行编辑模式
      await setFieldsValue(data);
      title.value = '修改盘点信息';
    }
  });
  let title = ref<string>('修改盘点信息');

  // 保存数据
  async function handleSubmit() {
    try {
      const formData = await validate();
      setModalProps({ confirmLoading: true });

      // const batchData = {
      //   ...formData,
      //   selectedIds: record.value.selectedKeys,
      //   selectedRows: record.value.selectedRows,
      // };
      // 根据选中的数据selectedKeys，来构造批量更新的数据
      const batchData = record.value.selectedKeys.map((row) => ({
        ...formData,
        id: row,
      }));
      console.log('批量更新的数据:', batchData);

      // 这里调用批量更新的 API
      const res = await savePdQtyData({
        whPdStockBillsList: batchData,
      });
      showMessage(res.message);

      showMessage(`成功更新 ${record.value.selectedRows.length} 条记录`);

      setTimeout(closeModal);
      emit('success', formData);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage('您填写的信息有误，请根据提示修正。');
      }
      console.log('error', error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
<style scoped>
  .ant-picker {
    width: 80%;
  }
</style>
